const pool = require("./db.js");

class CommentDao {
    async addComment(noticeID, userId, content,fID) {
        const [rows, fields] = await pool.execute(
            "INSERT INTO Comments (notice_id, user_id, comment_content, comment_time, comment_fID) VALUES (?, ?, ?, ?, ?)",
            [noticeID, userId, content, new Date(), fID]
        );
        return rows;
    }

    async deleteComment(id) {
        const [rows, fields] = await pool.execute("DELETE FROM Comments WHERE comment_id = ?", [id]);
        return rows;
    }

    async deleteCommentsByNoticeId(noticeId) {
        const [rows, fields] = await pool.execute("DELETE FROM Comments WHERE notice_id = ?", [noticeId]);
        return rows;
    }

    async selectCommentsByNoticeId(noticeId) {
        const [rows, fields] = await pool.execute(
            "SELECT DISTINCT * FROM Comments WHERE notice_id = ?",
            [noticeId]
        );
        return rows;
    }
}

module.exports = new CommentDao();